﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cosmetic.DataEntity;

namespace Cosmetic.Logic
{
	public class ContentManager : LogicBase
    {
		/// <summary>
		/// Insert a new Content into database
		/// </summary>
		/// <param name="aContent">new Content</param>
		public void Insert(Content aContent)
		{
			m_CosmeticDC.Contents.InsertOnSubmit(aContent);
			m_CosmeticDC.SubmitChanges();
		}

		/// <summary>
		/// Delete Content
		/// </summary>
		/// <param name="aContent">The Content want delete</param>
		public void Delete(Content aContent)
		{
			m_CosmeticDC.Contents.DeleteOnSubmit(aContent);
			m_CosmeticDC.SubmitChanges();
		}



		/// <summary>
		/// get content by content id
		/// </summary>
		/// <param name="aContentId">the specify content id</param>
		/// <returns></returns>
		public Content GetById(Guid aContentId)
		{
			List<Content> theContentList	= (from theContent in m_CosmeticDC.Contents
												   where theContent.ContentId == aContentId
												   select theContent).ToList();

			if (theContentList.Count == 0)
			{
				return null;
			}
			return theContentList[0];
		}

		/// <summary>
		/// get content by content category. ie:最新消息,美丽宝典...
		/// </summary>
		/// <param name="aCategory">the specify category</param>
		/// <returns>content list</returns>
		public List<Content> GetByCategory(Category aCategory)
		{
			return this.GetByCategory(aCategory.CategoryId);
		}
		/// <summary>
		/// get content by content category id. ie:最新消息,美丽宝典...
		/// </summary>
		/// <param name="aCategoryId">the specify category id</param>
		/// <returns>content list</returns>
		private List<Content> GetByCategory(Guid aCategoryId)
		{
			return (from theContent in m_CosmeticDC.Contents
						where theContent.ContentCategoryId == aCategoryId
						orderby theContent.CreateTime descending
						select theContent).ToList();
		}
	}
}
